define(function(require, exports){
	var $ = require('jquery');
	var pubjs = require('pubjs');
	var view  = require('@base/view');
	var util = require('util');
	var pop = require('admin/smartMonitor/popwin/popwin');

	// 新建/编辑广告监测
	var Edit = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': 'P-adminSmartMonitor',
				'tplFile':	'/tpl/monitor/edit.tpl',
				'hasCopyPublic': false,
				'hasFootBtn': true,
				'readonly': false, // 只读模式
				'steps': ['basic', 'product'],
				'url': {
					'planInfo': 'plan/info',
					'addMonitor':'campaign/add',
					'updateMonitor': 'campaign/update',
					'monitorInfo': 'campaign/info'
				},
				'vModel': {
					'STEPTEXT': [
						LANG('检测基本信息'),
						LANG('创意包与落地页')
					],
					'STEP': 0,
					'vmClickNext': this.eventNext,
					'vmClickCancel': this.eventCancel,
					'vmClickSave': this.eventSave,
					'vmClickPrev': this.eventPrev,

				},
				'layout': {
					'module': view.container,
					'step': false
				},
				'param': {
					Type: 2,// 2类型为代理
					_id: null
				}

			});
			this.$id = null;
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var c = self.getConfig();
			var el = self.getDOM();

			self.uiProxy(el, '.P-campaignEditTab .step li', 'click', 'eventGoStep');
			if(c.layout.step){
				el.prepend("<div pub-name='step' pub-mod='layout/navStep.base'></div>")
			}
			var config = {
				// 表单取消按钮
				'cancel': {
					'value': LANG('取消'),
					'class': 'btn btn-link mr20'
				},
				// 表单保存按钮
				'save': {
					'value': LANG('保存'),
					'class': 'btn btn-confirm'
				}
			}
			self.buildFromTemplate(config);
		},
		onDropChange: function(ev){
			if(ev.name == 'plan'){
				var id = +ev.param.id;
				this.loadPlan(id);
			}
		},
		loadPlan: function(id){
			var self = this;
			var c = self.getConfig();
			if(id){
				self.showLoading();
				pubjs.mc.send(c.url.planInfo, {_id: +id}, self.onPlanData.bind(self));
			}
		},
		onPlanData: function(err, data){
			this.hideLoading();
			if (err) {
				pubjs.error(err);
				pubjs.alert(err.message);
				return false;
			}

			this.$planData = data;
			this.cast('loadPlanData', data);
		},
		eventGoStep: function(evt,elm){
			var index = $(elm).attr('data-id');
			if(index >= this.getConfig().steps.length-1){
				this.vm.set('showSave', true);
			}
			this.vm.set({'STEP': index});
			return false;
		},
		eventNext: function(){
			this.next();
		},
		eventCancel: function(){
			this.cancel();
		},
		eventPrev: function(){
			this.prev();
		},
		eventSave: function(){
			this.save();
		},
		next: function(){
			var self = this;
			var c = self.getConfig();
			var index = ++self.vm.$.STEP;

			if(index >= c.steps.length-1){
				self.vm.set('showSave', true);
			}

			self.vm.set({'STEP': index});
		},
		cancel: function(){
			var self = this;
			pubjs.controller.navigate(-1);
		},
		prev: function(){
			this.vm.$.STEP--;
		},
		save: function(){
			if(!this.$.basic.validate() || !this.$.product.validate()){
				return false;
			}
			var c = this.getConfig();
			var param = this.getData();
			var url = null;
			var self = this;
			if(this.$id){
				url = this.getConfig().url.updateMonitor;
				param._id = this.$id;
			}
			else{
				url = this.getConfig().url.addMonitor;
			}
			pubjs.mc.send(url,param,function(err,data){
				if(err){
					pubjs.error(err.message);
					pubjs.alert(err.message);
					return false;
				}
				self.cancel();
			});
		},
		getData: function(){
			var self = this;
			var mod = self.$;
			var result = {
				Type: 2, //2为代理
				Status: 1
			};
			var product = mod.product.getData();
			var basic = mod.basic.getData();

			result = util.extend(product,result);
			result = util.extend(basic,result);
			return result;
		},
		setData: function(data){
			this.$.basic.setData(data);
			this.$.product.setData(data);
		},
		reset: function(){
			this.vm.$.STEP = 0;
			this.$id = null;
			this.$.basic.reset();
			this.$.product.reset();
		},
		load: function(id, isCopy){
			var self = this;
			var c = self.getConfig();
			if(!isCopy){
				this.$id = id;
			}
			pubjs.loading.show();
			pubjs.mc.send(c.url.monitorInfo,{'_id':id},self.onData.bind(this));

			return this;
		},
		onData: function(err,data){
			pubjs.loading.hide();
			if(err){
				return pubjs.alert(err.message);
			}
			this.setData(data);
		}
	});
	exports.edit = Edit;
});